package pe.com.cineplanet.dao.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import pe.com.cineplanet.bean.cineplanet.Perfil;
import pe.com.cineplanet.dao.dao.PerfilDAO;


public class PerfilDAOImpl implements PerfilDAO{

	protected JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public Perfil findByNombre(String nombre) {
		String sql = "select id, nombre from T_PERFIL where nombre=?";
		Object[] params = new Object[]{ nombre };
		
		try{
			return (Perfil) jdbcTemplate.queryForObject(sql, params, new RowMapper(){

				public Perfil mapRow(ResultSet rs, int arg1) throws SQLException {
					Perfil perfil = new Perfil();
					perfil.setId(rs.getInt(1));
					perfil.setNombre(rs.getString(2));
					return perfil;
				}
				
			});
		}catch(EmptyResultDataAccessException e){
			return null;
		}
	}

	public Perfil findById(int id) {
		String sql = "select id, nombre from T_PERFIL where id=?";
		Object[] params = new Object[]{ id };
		
		try{
			return (Perfil) jdbcTemplate.queryForObject(sql, params, new RowMapper(){

				public Perfil mapRow(ResultSet rs, int arg1) throws SQLException {
					Perfil perfil = new Perfil();
					perfil.setId(rs.getInt(1));
					perfil.setNombre(rs.getString(2));
					return perfil;
				}
				
			});
		}catch(EmptyResultDataAccessException e){
			return null;
		}
	}

}
